package com.Dao;

import com.utils.QueryRunnerUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class BaseDao {
    public static QueryRunner runner = null;

    public QueryRunner queryRunner = new QueryRunnerUtils().getQueryRunner();

    /**
     * 查询多条数据，每一条数据放入到Map中，每个map放入到list集合中
     * @param sql 查询sql
     * @param params 预编译参数
     */
    public List<Map<String, Object>> queryForListMap(String sql, Object...params) throws SQLException {
        return queryRunner.query(sql, new MapListHandler(), params);
    }

    /**
     * 查询单条数据，每一条数据放入到Map中
     * @param sql 查询sql
     * @param params 预编译参数
     */
    public Map<String, Object> queryForMap(String sql, Object...params) throws SQLException {
        return queryRunner.query(sql, new MapHandler(), params);
    }

    /**
     * 查询多条数据，将数据放入到实体类中，再将实体类放入到list中
     * @param sql
     * @param clas 类的反射
     * @param params 参数
     * @param <T> 实体类型
     */
    public <T> List<T> queryForListObject(String sql, Class<T> clas, Object...params) throws SQLException {
        return queryRunner.query(sql, new BeanListHandler<T>(clas), params);
    }

    /**
     * 查询单条数据放入到实体类中
     */
    public <T> T queryForObject(String sql, Class<T> clas, Object...params) throws SQLException {
        return queryRunner.query(sql, new BeanHandler<T>(clas), params);
    }
    public <T> List<T> findList(Class<T> beanClass, String sql) throws SQLException {
        return runner.query(sql, new BeanListHandler<T>(beanClass));
    }

    /**
     * 将输入放入到list集合中的对象数组
     * 每一个数组只有拿一条数据的值，没有列名
     * @return
     */
    public List<Object[]> getValuesForObject(String sql, Object...params) throws SQLException {
        return queryRunner.query(sql, new ArrayListHandler(), params);
    }

    /**
     * 取一条放入到对象数组
     */
    public Object[] getValueForObject(String sql, Object...params) throws SQLException {
        return queryRunner.query(sql, new ArrayHandler(), params);
    }

    /**
     * 取表对应的数据条数
     */
    public Long getTotalCount(String sql, Object...params) throws SQLException {
        return queryRunner.query(sql, new ScalarHandler<Long>(), params);
    }

    public <T> T findBean(Class<T> beanClass, String sql, Object... params) throws SQLException {
        return runner.query(sql, new BeanHandler<T>(beanClass), params);
    }


    public <T> T findBean(Class<T> beanClass, String sql){
        try {
            return runner.query(sql, new BeanHandler<T>(beanClass));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    //查询多条数据放到实体类中
    public <T> List<T> findList(Class<T> beanClass, String sql, Object... params){
        try {
            return runner.query(sql, new BeanListHandler<T>(beanClass),params);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }



    public long getCount(String sql, Object... params){
        try {
            return  runner.query(sql, new ScalarHandler<Long>(), params);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }


    public int count(String sql){
        try {
            return runner.query(sql, new ScalarHandler<Integer>());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }


    public int update(String sql, Object... params){
        try {
            return runner.update(sql, params);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }


    public int[] batch(String sql, Object[][] params) {
        try {
            return runner.batch(sql, params);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


}
